package com.lagou.SystemView;

import com.lagou.QuestionsSystem.StudentsExamSystem;
import com.lagou.client.ClientInitClose;
import com.lagou.client.ClientScanner;
import com.lagou.model.User;

import java.io.IOException;

/**
 * 编程实现学员系统界面的绘制和功能实现
 */
public class StudentsView {

    /**
     * 合成复用原则
     */
    private ClientInitClose cic;
    private User user;

    /**
     * 通过构造方法实现成员变量的初始化
     */
    public StudentsView(ClientInitClose cic, User user){
        this.cic = cic;
        this.user = user;
    }

    //绘制学员系统界面
    public void StudentsMainPage() throws IOException, ClassNotFoundException {

        while (true) {
            System.out.println("   \n\n          学员系统");
            System.out.println("--------------------------------");
            System.out.print("   [1] 修改密码");
            System.out.println("   [2] 考试系统");
            System.out.println("   [0] 退出");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    cic.getPs().println("setPassword");
                    setPassword();
                    break;
                case 2:
                    cic.getPs().println("examSystem");
                    //声明StudentsExamSystem的引用指向该类型的对象
                    StudentsExamSystem ses = new StudentsExamSystem(cic);
                    //进入考试系统
                    ses.examSystemView();
                    break;
                case 0:
                    cic.getPs().println("exit");
                    System.out.println("正在退出系统");
                    return;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }


    /**
     * 编程实现学生修改密码功能
     */
    public void setPassword() throws IOException {
        //发送用户名到客户端
        cic.getPs().println(user.getUserName());
        //
        System.out.println("请输入需要修改后的密码");
        cic.getPs().println(ClientScanner.getScanner().next());
        //获取服务器返回的信息
        String s = cic.getBr().readLine();
        if("success".equals(s)){
            System.out.println("修改密码成功");
        }else if("fail".equals(s)){
            System.out.println("修改密码失败");
        }
    }
}
